package com.multitrack.map.datatype;

import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;


public class Polyline extends Overlay {
	
	private GeoPoints points; 
	private Paint pathPaint = null;	
	
	final static int strokeWidth = 4;
	final static int alpha = 100;
	final static int red = 113;
	final static int green = 105;
	final static int blue = 252;
	
	
	public Polyline() {
		this.points = new GeoPoints();
		this.pathPaint = new Paint();
		this.pathPaint.setAntiAlias(true);
	}
	
	public Polyline(GeoPoints geopoint) {
		
		this.points = geopoint;		
		this.pathPaint = new Paint();
		this.pathPaint.setAntiAlias(true);
		
	}
	
	public void setGeoPoints(GeoPoints points) {
		this.points.clear();
		this.points = points;
	}
	
	public GeoPoint get(int index) {
		return this.points.get(index);
	}
	
	public int size() {
		return this.points.size();
	}
	
	@Override
	public void draw(Canvas canvas, MapView view, boolean shadow) {
		super.draw(canvas, view, shadow);
		
		if( points == null) {
			return;
		}
		
		Projection projection = view.getProjection();
		
		this.pathPaint.setStrokeWidth(strokeWidth);
		this.pathPaint.setARGB(alpha, red, green, blue);
		this.pathPaint.setStyle(Paint.Style.STROKE);
		
		Path routePath = new Path();
			
		Iterator<GeoPoint> it = points.iterator();
			
		Point first = null;
		first = projection.toPixels(it.next(), first);
		routePath.moveTo(first.x, first.y);
		
		
		while(it.hasNext()) {
						
			Point outPoint = null;
			outPoint = projection.toPixels(it.next(), outPoint);
			
			routePath.lineTo(outPoint.x, outPoint.y);
			routePath.moveTo(outPoint.x, outPoint.y);
		}
		
		canvas.drawPath(routePath, pathPaint);
		
	}

	public boolean add(GeoPoint geoPoint) {
		return this.points.add(geoPoint);
	}
	
	public void clear() {
		this.points.clear();
	}
	
}


